﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using NetJsWire.Lite.Converters;
using NetJsWire.Lite.Properties;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Form
{
    [JsonObject( MemberSerialization = MemberSerialization.OptIn )]
    public class CheckBoxLW : FieldLW
    {
        private bool _checked;

        public CheckBoxLW()
            : this( null )
        {
        }

        public CheckBoxLW( IContainer container )
            : base( container )
        {
            base.Xtype = "checkbox";
            base.Height = 20;
            base.MinimumSize = new Size( 24, 20 );
        }

        [JsonProperty( "boxLabel", DefaultValueHandling = DefaultValueHandling.Ignore, NullValueHandling = NullValueHandling.Ignore )]
        [DefaultValue( ( string ) null )]
        [RefreshProperties( RefreshProperties.Repaint )]
        public string BoxLabel
        {
            get { return base.Text; }
            set
            {
                base.Text = value;
                Invalidate();
            }
        }

        [JsonProperty( "checked", DefaultValueHandling = DefaultValueHandling.Ignore )]
        [DefaultValue( false )]
        [Description( "true if the checkbox should render initially checked." )]
        [RefreshProperties( RefreshProperties.Repaint )]
        public bool Checked
        {
            get { return _checked; }
            set
            {
                _checked = value;
                Invalidate();
            }
        }

        [Browsable( true )]
        [JsonProperty( "handler" )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [Editor( typeof( MultilineStringEditor ), typeof( UITypeEditor ) )]
        [DefaultValue( ( string ) null )]
        [Description( "A function called when the checked value changes (can be used instead of handling the check event). The handler is passed the following parameters:\ncheckbox : Ext.form.Checkbox\nThe Checkbox being toggled.\nchecked : Boolean\nThe new checked state of the checkbox." )]
        public string Handler { get; set; }

        [Browsable( true )]
        [JsonProperty( "inputValue" )]
        [DefaultValue( ( string ) null )]
        [Description( "The value that should go into the generated input element's value attribute" )]
        public string InputValue { get; set; }

        protected override void OnPaint( PaintEventArgs e )
        {
            e.Graphics.DrawImage( ( Checked ? Resources.Checkbox_checked : Resources.Checkbox_unchecked ), e.ClipRectangle.X, e.ClipRectangle.Y + ( ( Height / 2 ) - ( Resources.Checkbox_unchecked.Height / 2 ) ) );
            if (string.IsNullOrWhiteSpace(BoxLabel)) return;

            using ( var font = new Font( "Tahoma", 11, FontStyle.Regular, GraphicsUnit.Pixel ) )
            {
                using ( var br = new SolidBrush( Color.Black ) )
                {
                    var sf = new StringFormat
                                 {
                                     LineAlignment = StringAlignment.Center,
                                     Trimming = StringTrimming.Character
                                 };
                    var textBounds = new Rectangle( e.ClipRectangle.X + Resources.Checkbox_checked.Width + 5,
                                       e.ClipRectangle.Y, e.ClipRectangle.Width - ( Resources.Checkbox_checked.Width + 5 ), e.ClipRectangle.Height );
                    //var textSize = e.Graphics.MeasureString( BoxLabel, font );
                    e.Graphics.DrawString( BoxLabel, font, br, textBounds, sf );
                }
            }
        }
    }
}
